package com.bjsxt.shop.common.util;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.OutputStream;
import java.util.Iterator;
import java.util.List;

import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.apache.poi.ss.usermodel.Cell;
import org.apache.poi.ss.usermodel.Row;
import org.apache.poi.ss.usermodel.Sheet;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.stereotype.Component;

/**
 * poi上传下载数据封装类
 * @author WYN
 *
 */
@Component("poiUtil")
public class PoiUtil {
	//
	public void writeFile(List<List<String>> dataList,OutputStream os){
		Workbook wb = new HSSFWorkbook();
		try
		{
			/* 参数为:sheet的名字 */
			Sheet sheet = wb.createSheet();
			int rowCount = 0 ; 
			for (Iterator iterator = dataList.iterator(); iterator.hasNext();)
			{
				List<String> list = (List<String>) iterator.next();
				/*
				 * 参数:表示第几行
				 */
				Row row = sheet.createRow(rowCount);
				
				int cellCount = 0 ; 
				/* 表示单元格 */
				for (Iterator iterator2 = list.iterator(); iterator2.hasNext();)
				{
					String value = (String) iterator2.next();
					
					/* 单元格 
					 * 参数:第几列*/
					Cell cell1 = row.createCell(cellCount);
					cell1.setCellValue(value);
					cellCount ++ ; 
				}
				rowCount ++ ; 
			}
			/* 写入文件 */
			wb.write(os);
			/* 关闭流 */
			os.close();
		} catch (FileNotFoundException e)
		{
			e.printStackTrace();
		} catch (IOException e)
		{
			e.printStackTrace();
		}
	}
}
